﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Hxsoft.AppV1
{
	/// <summary>
	/// 分页信息
	/// </summary>
	/// <typeparam name="T">分页的信息类型</typeparam>
	public class PaginationInfo<T> where T : ICollection, IEnumerable, new()
	{
		private int _currentPage;
		private int _pageSize;
		private int _recordCount;
		private T _records = new T();

		/// <summary>
		/// 当前页
		/// </summary>
		public int CurrentPage
		{
			get { return _currentPage; }
			set { _currentPage = value; }
		}

		/// <summary>
		/// 页尺寸
		/// </summary>
		public int PageSize
		{
			get { return _pageSize; }
			set { _pageSize = value; }
		}

		/// <summary>
		/// 记录总数
		/// </summary>
		public int RecordCount
		{
			get { return _recordCount; }
			set { _recordCount = value; }
		}

		/// <summary>
		/// 当前页的记录
		/// </summary>
		public T Records
		{
			get { return _records; }
			set { _records = value; }
		}

		/// <summary>
		/// 页总数
		/// </summary>
		public int PageCount
		{
			get
			{
				double d = (double)RecordCount / (double)PageSize;
				int pageCount = (int)d;
				if (d > pageCount)
					pageCount += 1;
				return pageCount;
			}
		}

		/// <summary>
		/// 当前页的页界限
		/// </summary>
		public PageLimit CurrentPageLimit
		{
			get
			{
				PageLimit limit = new PageLimit();

				limit.Lower = (CurrentPage - 1) * PageSize + 1;
				limit.Upper = limit.Lower + Records.Count - 1;

				return limit;
			}
		}

		/// <summary>
		/// 页界限
		/// </summary>
		public class PageLimit
		{
			private int _lower;
			private int _upper;

			/// <summary>
			/// 下限
			/// </summary>
			public int Lower
			{
				get { return _lower; }
				set { _lower = value; }
			}

			/// <summary>
			/// 上限
			/// </summary>
			public int Upper
			{
				get { return _upper; }
				set { _upper = value; }
			}
		}
	}
}
